//
//  BaseNavigationBar.swift
//  BaseViewControllerKit
//
//  Created by Max Mg on 2024/1/28.
//

import UIKit
import ExtensionsKit

public protocol BaseNavigationBarItem {
    var navBarItemView: UIView { get }
}

extension UIView: BaseNavigationBarItem {
    public var navBarItemView: UIView {
        return self
    }
}

open class BaseNavigationBar: UIView {
    public lazy var contentView = UIView()
    public lazy var leftContentView = UIView()
    public lazy var rightContentView = UIView()
    public var actionHandler: ((BaseNavigationBarItem) -> Void)?
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        
        addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.top.equalTo(safeAreaLayoutGuide.snp.top).offset(-44)
            make.height.equalTo(44.0)
            make.leading.trailing.bottom.equalToSuperview()
        }
        leftContentView.backgroundColor = .clear
        contentView.addSubview(leftContentView)
        leftContentView.snp.makeConstraints { make in
            make.leading.top.bottom.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.5)
        }
        rightContentView.backgroundColor = .clear
        contentView.addSubview(rightContentView)
        rightContentView.snp.makeConstraints { make in
            make.trailing.top.bottom.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.5)
        }
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public private(set) lazy var leftItems = [BaseNavigationBarItem]()
    public func updateLeftItems(_ items: [BaseNavigationBarItem]) {
        leftContentView.subviews.forEach({ $0.removeFromSuperview() })
        leftItems = items
        
        var leading: CGFloat = 12
        for (index, item) in items.enumerated() {
            let view = item.navBarItemView
            view.tag = index+100
            leftContentView.addSubview(view)
            view.snp.makeConstraints { make in
                make.leading.equalTo(leading)
                make.centerY.equalToSuperview()
                make.size.equalTo(view.frame.size)
            }
            leading += view.width
            leading += 12
            if let button = view as? UIControl {
                button.addTarget(self, action: #selector(itemClick(_:)), for: .touchUpInside)
            }
        }
    }
    
    private lazy var rightItems = [BaseNavigationBarItem]()
    public func updateRightItems(_ items: [BaseNavigationBarItem]) {
        rightContentView.subviews.forEach({ $0.removeFromSuperview() })
        rightItems = items
        
        var trailing: CGFloat = -6
        for (index, item) in items.enumerated() {
            let view = item.navBarItemView
            view.tag = index+200
            rightContentView.addSubview(view)
            view.snp.makeConstraints { make in
                make.trailing.equalTo(trailing)
                make.centerY.equalToSuperview()
                make.size.equalTo(view.frame.size)
            }
            trailing -= view.width
            trailing -= 6
            if let button = view as? UIControl {
                button.addTarget(self, action: #selector(itemClick(_:)), for: .touchUpInside)
            }
        }
    }
    
    private weak var centerView: UIView?
    public func updateCenterItem(_ item: BaseNavigationBarItem) {
        centerView?.removeFromSuperview()
        
        let view = item.navBarItemView
        contentView.addSubview(view)
        view.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        centerView = view
    }
    
    public var titleColor: UIColor = UIColor.label
    
    public func updateTitle(_ title: String?, font: UIFont) {
        let label = UILabel()
        label.font = font
        label.text = title
        label.textColor = titleColor
        label.sizeToFit()
        updateCenterItem(label)
    }
    
    var currentTitle: String? {
        return (centerView as? UILabel)?.text
    }
    
    @objc private func itemClick(_ sender: UIControl) {
        if sender.tag < 200 {
            if let item = leftItems.value(at: sender.tag - 100) {
                actionHandler?(item)
            }
        } else {
            if let item = rightItems.value(at: sender.tag - 200) {
                actionHandler?(item)
            }
        }
    }
}
